package com.justech.eat.service.impl;

import com.justech.common.util.DateUtil;
import com.justech.eat.entity.OrderDetailAbnormalRecord;
import com.justech.eat.repository.OrderDetailAbnormalRecordRepository;
import com.justech.eat.service.OrderDetailAbnormalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: eat-service
 * @description:
 * @author: feifei.li
 * @create: 2020-06-08 18:19
 **/
@Service
public class OrderDetailAbnormalServiceImpl implements OrderDetailAbnormalService {

    @Autowired
    OrderDetailAbnormalRecordRepository orderDetailAbnormalRecordRepository;

    @Override
    public OrderDetailAbnormalRecord add(Long orderId, Long orderDetailId, String jobNumber) {
        OrderDetailAbnormalRecord recordRepositoryByOrderIdAndOrderDetailId = orderDetailAbnormalRecordRepository.findByOrderIdAndOrderDetailId(orderId, orderDetailId);
        if (recordRepositoryByOrderIdAndOrderDetailId == null) {
            OrderDetailAbnormalRecord abnormalRecord = new OrderDetailAbnormalRecord();
            abnormalRecord.setStatus(0);
            abnormalRecord.setJobNumber(jobNumber);
            abnormalRecord.setOrderDetailId(orderDetailId);
            abnormalRecord.setOrderId(orderId);
            abnormalRecord.setCreateTime(DateUtil.now());
            return orderDetailAbnormalRecordRepository.saveAndFlush(abnormalRecord);
        }
        return recordRepositoryByOrderIdAndOrderDetailId;
    }

    @Override
    public OrderDetailAbnormalRecord add(Long orderId, Long orderDetailId, String jobNumber, int type) {
        OrderDetailAbnormalRecord recordRepositoryByOrderIdAndOrderDetailId = orderDetailAbnormalRecordRepository.findByOrderIdAndOrderDetailId(orderId, orderDetailId);
        if (recordRepositoryByOrderIdAndOrderDetailId == null) {
            OrderDetailAbnormalRecord abnormalRecord = new OrderDetailAbnormalRecord();
            abnormalRecord.setStatus(0);
            abnormalRecord.setType(type);
            abnormalRecord.setJobNumber(jobNumber);
            abnormalRecord.setOrderDetailId(orderDetailId);
            abnormalRecord.setOrderId(orderId);
            abnormalRecord.setCreateTime(DateUtil.now());
            return orderDetailAbnormalRecordRepository.saveAndFlush(abnormalRecord);
        }
        return recordRepositoryByOrderIdAndOrderDetailId;
    }
}
